﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace Web.Views.Portal
{
    public partial class GetNavigateMenu : PageBase
    {
        private IMDataHelper dal = null;
        //private string loadingUrl = "Views/SystemLogin/loadingpage.aspx";
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //获得父ID
            dal = new IMDataHelper();
            //获取子节点的json信息
            string sjson = GetPortalMenuData();
            if (!string.IsNullOrEmpty(sjson))
            {
                this.Response.Write(sjson);
            }
        }


        /// <summary>
        /// 获取门户导航信息
        /// </summary>
        /// <param name="parent_Id"></param>
        /// <returns></returns>
        public string GetPortalMenuData()
        {
            ////选出所有的菜单
            string sql = "  SELECT a.Menu_ID  menuId,a.Parent_Menu_ID  parentMenuId,a.Menu_URL  menuUrl,a.Order_Num  menuOrder,b.vDesc  menuText,a.IsPublicMenu ,b.vCulture FROM P_Auth_Menu  a join (select * from Config_Res_System where vStringKey in (SELECT Menu_ID FROM P_Auth_Menu))  b on a.MENU_ID =b.vStringKey where b.vPageUrl = 'Public/Modules' and vCulture='zh-CN' ";
            sql = sql + " order by Order_Num desc";
            DataTable dt = dal.DBHelper.ExecuteDataSet(sql).Tables[0];
            DataTable dtMenu = dt.Clone();
            {
                DataTable dtCopy = dt.Clone();
                foreach (DataRow row in dt.Rows)
                {
                    string Id = row["menuId"].ToString();
                    string _menuUrl = row["menuUrl"].ToString();
                    if (_menuUrl != string.Empty)
                    {
                        //判断权限
                        bool hasAuthority = (this.HasAuthority("ViewAll", Id) ||
                           this.HasAuthority("ViewDeptAll", Id) ||
                           this.HasAuthority("ViewDept", Id) ||
                           this.HasAuthority("ViewCreate", Id));
                        //如果没有权限，则删除复制的表中的该字段
                        if (hasAuthority)
                        {
                            DataRow ImpRow = dt.Select("menuId='" + Id + "'")[0];
                            //dtCopy.Rows.Remove(deleteRow);
                            //dtCopy.
                            dtCopy.ImportRow(ImpRow);
                        }
                    }
                    //如果是没有Url 的，判断下面是否有子节点，有的话 就不删除
                    else
                    {
                        //找出子节点，如果子节点删除光了，则删除这个节点
                        DataRow[] rowsClild = dtCopy.Select("parentMenuId='" + Id + "'");
                        if (rowsClild.Length > 0)
                        {
                            DataRow ImpRow = dt.Select("menuId='" + Id + "'")[0];
                            dtCopy.ImportRow(ImpRow);
                        }
                    }

                }


                DataRow[] rows = dtCopy.Select("parentMenuId='ePlanet'");//选出有权限的列
                DataRow[] rowP = dt.Select("menuId='ePlanet.Desktop'");//选出首页
                //加入有权限的列
                foreach (DataRow row in rows)
                {
                    dtMenu.ImportRow(row);
                }
                //加入首页
                foreach (DataRow row in rowP)
                {
                    dtMenu.ImportRow(row);
                }
            }
            //做完这些操作，再组织数据
            DataView dv = dtMenu.DefaultView;
            dv.Sort = "menuOrder asc";
            dtMenu = dv.ToTable();
            for (int i = 0; i < dtMenu.Rows.Count; )
            {
                if (dtMenu.Rows[i]["IsPublicMenu"].Equals(false))
                {
                    dtMenu.Rows.Remove(dtMenu.Rows[i]);
                }
                else
                {
                    i++;
                }
            }
            dtMenu.AcceptChanges();
            return dtMenu.ToJson();
        }
    }
}
